<?php

class Agilet_Exception extends Exception
{

    /**
     * @var  array  PHP error code => human readable name
     */
    public static $php_errors = array(
        E_ERROR => 'Fatal Error',
        E_USER_ERROR => 'User Error',
        E_PARSE => 'Parse Error',
        E_WARNING => 'Warning',
        E_USER_WARNING => 'User Warning',
        E_STRICT => 'Strict',
        E_NOTICE => 'Notice',
        E_RECOVERABLE_ERROR => 'Recoverable Error',
    );

    /**
     * Creates a new translated exception.
     *
     *     throw new Kohana_Exception('Something went terrible wrong, :user',
     *         array(':user' => $user));
     *
     * @param   string          error message
     * @param   array           translation variables
     * @param   integer|string  the exception code
     * @return  void
     */
    public function __construct($message, array $variables = NULL, $code = 0)
    {

        if ($variables && is_array($variables))
        {
            // Set the message
            $message = sprintf($message, $variables);
        }

        // Pass the message and integer code to the parent
        parent::__construct($message, (int)$code);

        // Save the unmodified code
        // @link http://bugs.php.net/39615
        $this -> code = $code;
    }

    /**
     * Magic object-to-string method.
     *
     *     echo $exception;
     *
     * @uses    Kohana_Exception::text
     * @return  string
     */
    public function __toString()
    {
        return self::text($this);
    }

    /**
     * Inline exception handler, displays the error message, source of the
     * exception, and the stack trace of the error.
     *
     * @uses    Kohana_Exception::text
     * @param   object   exception object
     * @return  boolean
     */
    public static function __handle_exception(Exception $e)
    {
        try
        {
            // Get the exception information
            $type = get_class($e);
            $code = $e -> getCode();
            $message = $e -> getMessage();
            $file = $e -> getFile();
            $line = $e -> getLine();

            // Get the exception backtrace
            $trace = $e -> getTrace();

            // Create a text version of the exception
            $error = self::text($e);

            echo $error;
        }
        catch (Exception $e)
        {
            // Clean the output buffer if one exists
            ob_get_level() and ob_clean();

            // Display the exception text
            echo self::text($e), "\n";

            // Exit with an error status
            exit(1);
        }
    }

    /**
     * Get a single line of text representing the exception:
     *
     * Error [ Code ]: Message ~ File [ Line ]
     *
     * @param   object  Exception
     * @return  string
     */
    public static function text(Exception $e)
    {
        return sprintf('%s [ %s ]: %s ~ %s [ %d ]', get_class($e), $e -> getCode(), strip_tags($e -> getMessage()), $e -> getFile(), $e -> getLine());
    }

}
